All files / Bandstand/src/app/api/cues/[id] route.ts

0% Statements 0/51
0% Branches 0/1
0% Functions 0/1
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
export const dynamic = 'force-dynamic'

import {z} from 'zod'
import {prisma} from '@/lib/db'
import {requireUser} from '@/lib/guard'
import {toCue} from '@/lib/cues-server'
import {route} from '@/lib/route'

type Ctx = {params: {id: string}}

const Patch = z.object({
  text: z.string().trim().max(5000).nullable().optional(),
  anchor: z
    .string()
    .regex(/^$|^[^#]{1,80}#\d{1,3}$/)
    .optional(),
  position: z.number().int().min(0).max(1000).optional(),
})

/** Only ever your own cues: anyone else's is "not found". */
async function mine(id: string, userId: string) {
  return prisma.cue.findFirst({
    where: {id, userId},
    select: {id: true, kind: true},
  })
}

export const PATCH = route(async (req: Request, {params}: Ctx) => {
  const {user} = await requireUser()
  const cue = await mine(params.id, user.id)
  if (!cue) return new Response('Not Found', {status: 404})
  const parsed = Patch.safeParse(await req.json())
  if (!parsed.success) return new Response('Bad Request', {status: 400})
  const {text, ...rest} = parsed.data
  if (text !== undefined && !text && cue.kind !== 'IMAGE')
    return new Response('Bad Request', {status: 400})
  const updated = await prisma.cue.update({
    where: {id: cue.id},
    data: {...rest, ...(text !== undefined ? {text: text || null} : {})},
    include: {image: {select: {width: true, height: true}}},
  })
  return Response.json(toCue(updated))
})

export const DELETE = route(async (_req: Request, {params}: Ctx) => {
  const {user} = await requireUser()
  const {count} = await prisma.cue.deleteMany({
    where: {id: params.id, userId: user.id},
  })
  return new Response(null, {status: count ? 204 : 404})
})